﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Text.RegularExpressions;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views;
using DevExpress.XtraGrid.Views.Base;
using System.Security.Cryptography;

namespace SLMNG
{
    public class Global
    {
        #region Format

        public static string UserID = "ADMIN";
        public static string UserCode = "ADMIN";
        public static string DateTimeFormat = "dd/MM/yyyy";
        public static string NumberFormatDefault = "N0";
        public static string CurrencyFormat = "0,0";
        public const String FORMAT_DATE = "dd/MM/yyyy";
        public const String FORMAT_TIME = "HH:mm";
        public const String FORMAT_NUMBER = "###,###,###,##0.####";

        public struct NumberFormat
        {
            public const string Integer = "###,###,###,##0";
            public const string IntegerWithNull = "###,###,###,###";
            public const string Decimal = "###,###,###,##0.00";
            public const string DecimalWithNull = "###,###,###,###.##";
            public const string Double = "###,###,###,##0.000";
            public const string DoubleWithNull = "###,###,###,##0.###";
            public const string Percent = "###,###,###,##0.00%";
            public const string PercentWithNull = "###,###,###,###.##%";
            public const string Other = "###,###,###,##0.0";
        }

        #endregion

        #region Auto generate number column

        //public static void OpenTabPages(int key, DevComponents.DotNetBar.Office2007RibbonForm frm)
        //{
        //    FrmMain frmM = (FrmMain)Application.OpenForms["FrmMain"];
        //    frmM.OpenTab(key, frm);
        //}

        public static void GenerateNumber( DataGridView grid, int col)
        {
            for (int i = 0; i < grid.Rows.Count; i++)
            {
                grid.Rows[i].Cells[col].Value = i + 1;
            }
        }

        //public static void GenerateEditColumn(DevComponents.DotNetBar.Controls.DataGridViewX grid, int col)
        //{
        //    for (int i = 0; i < grid.Rows.Count; i++)
        //    {
        //        grid.Rows[i].Cells[col].Value = Resource.edit_1_;
        //        grid.Rows[i].Cells[col].ToolTipText = "Sửa";
        //    }
        //}

        //public static void GenerateLineAndAction(DevComponents.DotNetBar.Controls.DataGridViewX grid, bool isLine, int colLine, bool isEdit, int colEdit, bool isDel, int colDel)
        //{
        //    for (int i = 0; i < grid.Rows.Count; i++)
        //    {
        //        if (isLine)
        //        {
        //            grid.Rows[i].Cells[colLine].Value = i + 1;
        //        }

        //        if (isEdit)
        //        {
        //            grid.Rows[i].Cells[colEdit].Value = Resource.edit_1_;
        //            grid.Rows[i].Cells[colEdit].ToolTipText = "Sửa";
        //        }

        //        if (isDel)
        //        {
        //            grid.Rows[i].Cells[colDel].Value = Resource.deletered;
        //            grid.Rows[i].Cells[colDel].ToolTipText = "Xóa";
        //        }
        //    }
        //}

        //public static void GenerateDeleteColumn(DevComponents.DotNetBar.Controls.DataGridViewX grid, int col)
        //{
        //    for (int i = 0; i < grid.Rows.Count; i++)
        //    {
        //        grid.Rows[i].Cells[col].Value = Resource.deletered;
        //        grid.Rows[i].Cells[col].ToolTipText = "Xóa";
        //    }
        //}

        //public static void SetMessage(ToolStripStatusLabel label, string message, bool isSuccess)
        //{
        //    if (isSuccess)
        //    {
        //        label.ForeColor = Color.Green;
        //        label.Text = message;
        //    }
        //    else
        //    {
        //        label.ForeColor = Color.Red;
        //        label.Text = message;
        //    }
        //}

        //public static DialogResult ShowMessageBoxDelete(string mess)
        //{
        //    return DevComponents.DotNetBar.MessageBoxEx.Show(mess, "Cảnh báo", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);
        //}

        #endregion

        #region Parse

        public static bool TestEmailRegex(string emailAddress)
        {
            // string patternLenient = @"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";
            // Regex reLenient = new Regex(patternLenient);
            //  bool isLenientMatch = reLenient.IsMatch(emailAddress
            // return isLenientMatch;

            string patternStrict = @"^(([^<>()[\]\\.,;:\s@\""]+"

                  + @"(\.[^<>()[\]\\.,;:\s@\""]+)*)|(\"".+\""))@"

                  + @"((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}"

                  + @"\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+"

                  + @"[a-zA-Z]{2,}))$";

            Regex reStrict = new Regex(patternStrict);
            bool isStrictMatch = reStrict.IsMatch(emailAddress);
            return isStrictMatch;
        }

        public static bool NumberOnlyRegex(string number)
        {
            string patternStrict = @"[0-9]{6,15}";

            Regex reStrict = new Regex(patternStrict);
            bool isStrictMatch = reStrict.IsMatch(number);
            return isStrictMatch;
        }

        public static bool boolParse(object o)
        {
            if (o == null)
                return false;

            bool r = false;

            bool.TryParse(o.ToString(), out r);

            return r;
        }

        public static int intParse(object obj)
        {
            int num = 0;
            if (obj == null)
            {
                return 0;
            }

            if (int.TryParse(obj.ToString(), out num))
            {
                return num;
            }
            else
            {
                return 0;
            }
        }

        public static DateTime datetimeParse(object obj)
        {
            DateTime dt = new DateTime();
            DateTime.TryParse(obj.ToString(), out dt);
            return dt;
        }

        public static float floatParse(object obj)
        {
            float f = 0;
            if (obj == null)
            {
                return 0;
            }

            if (float.TryParse(obj.ToString(), out f))
            {
                return f;
            }
            else
            {
                return 0;
            }
        }

        public static decimal decimalParse(object obj)
        {
            decimal d = 0;
            if (obj == null)
            {
                return 0;
            }

            if (decimal.TryParse(obj.ToString(), out d))
            {
                return d;
            }
            else
            {
                return 0;
            }
        }

        public static string stringParse(object obj)
        {
            string str = string.Empty;
            if (obj == null)
            {
                return string.Empty;
            }
            else
            {
                return obj.ToString();
            }
        }

        #endregion 

        #region Other

        public static DateTime CombineDateAndTime(object date, object time)
        {
            DateTime d = (DateTime)date;
            DateTime t = (DateTime)time;
            DateTime result = new DateTime(d.Year, d.Month, d.Day, t.Hour,t.Minute,t.Second);
            return result;
        }

        /// <summary>
        /// Giá trị mặc định cho việc Sleep 1 thread
        /// </summary>
        public static readonly int DEFAULT_SLEEPTIME = 100;

        //public static void GridEXInitialProperties(ref GridControl objGrid, bool bitAllowAdd, int intFrozenColumns)
        //{
        //    objGrid.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(255)))), ((int)(((byte)(192)))));
        //    //objGrid.AllowAddNew = bitAllowAdd;
        //    //objGrid.AllowDelete = InheritableBoolean.False;
        //    //objGrid.FrozenColumns = intFrozenColumns;
        //    //objGrid.EmptyRows = true;
        //    //objGrid.EnterKeyBehavior = EnterKeyBehavior.NextCell;
        //    //objGrid.NewRowEnterKeyBehavior = NewRowEnterKeyBehavior.Default;
        //    //GridEXGroupHeaderTotal ghTotal = new GridEXGroupHeaderTotal(null, AggregateFunction.Count);
        //    //ghTotal.TotalFormatString = NumberFormat.DecimalWithNull;
        //    //objGrid.RootTable.GroupHeaderTotals.Add(ghTotal);
        //    //objGrid.RecordNavigator = true;

        //    //if (filter != null)
        //    //{
        //    //    filter.BuiltInTextsData = Messages.LANGUAGE_FILTER_EDITOR_BUILDINTEXT;
        //    //    //Thiết lập minsize
        //    //    filter.MinSize = new Size(16, 45);
        //    //}

        //    //Format value in column
        //    foreach (DevExpress.XtraGrid.Columns.GridColumn _col in ((ColumnView)objGrid.Views[0]).Columns)
        //    {
        //        switch (Type.GetTypeCode(_col.ColumnType))
        //        {
        //            case TypeCode.Boolean:
        //                break;
        //            case TypeCode.Byte:
        //                {
        //                    _col.AppearanceCell.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
        //                    break;
        //                }
        //            case TypeCode.Char:
        //                break;
        //            case TypeCode.DBNull:
        //                break;
        //            case TypeCode.DateTime:
        //                _col.DisplayFormat.FormatString = Global.FORMAT_DATE;
        //                break;
        //            case TypeCode.Decimal:
        //                {
        //                    if (_col.DisplayFormat.FormatString == string.Empty || _col.DisplayFormat.FormatString.Equals("c")) 
        //                        _col.DisplayFormat.FormatString = Global.NumberFormat.DecimalWithNull;
        //                    if (_col.edit == string.Empty) _col.InputMask = "Number";
        //                        _col.TextAlignment = TextAlignment.Far;
        //                    break;
        //                }
        //            case TypeCode.Double:
        //                {
        //                    if (_col.FormatString == string.Empty) _col.FormatString = Globals.NumberFormat.DecimalWithNull;
        //                    if (_col.InputMask == string.Empty) _col.InputMask = "Number";
        //                    _col.TextAlignment = TextAlignment.Far;
        //                    break;
        //                }
        //            case TypeCode.Empty:
        //                break;
        //            case TypeCode.Int16:
        //                {
        //                    if (_col.FormatString == string.Empty) _col.FormatString = Globals.NumberFormat.IntegerWithNull;
        //                    if (_col.InputMask == string.Empty) _col.InputMask = "Number0";
        //                    _col.TextAlignment = TextAlignment.Far;
        //                    break;
        //                }
        //            case TypeCode.Int32:
        //                {
        //                    if (_col.FormatString == string.Empty) _col.FormatString = Globals.NumberFormat.IntegerWithNull;
        //                    if (_col.InputMask == string.Empty) _col.InputMask = "Number0";
        //                    _col.TextAlignment = TextAlignment.Far;
        //                    break;
        //                }
        //            case TypeCode.Int64:
        //                {
        //                    if (_col.FormatString == string.Empty) _col.FormatString = Globals.NumberFormat.IntegerWithNull;
        //                    if (_col.InputMask == string.Empty) _col.InputMask = "Number0";
        //                    _col.TextAlignment = TextAlignment.Far;
        //                    break;
        //                }
        //            case TypeCode.Object:
        //                break;
        //            case TypeCode.SByte:
        //                break;
        //            case TypeCode.Single:
        //                {
        //                    if (_col.FormatString == string.Empty) _col.FormatString = Globals.NumberFormat.DecimalWithNull;
        //                    if (_col.InputMask == string.Empty) _col.InputMask = "Number";
        //                    _col.TextAlignment = TextAlignment.Far;
        //                    break;
        //                }
        //            case TypeCode.String:
        //                break;
        //            case TypeCode.UInt16:
        //                {
        //                    if (_col.FormatString == string.Empty) _col.FormatString = Globals.NumberFormat.IntegerWithNull;
        //                    if (_col.InputMask == string.Empty) _col.InputMask = "Number0";
        //                    _col.TextAlignment = TextAlignment.Far;
        //                    break;
        //                }
        //            case TypeCode.UInt32:
        //                {
        //                    if (_col.FormatString == string.Empty) _col.FormatString = Globals.NumberFormat.IntegerWithNull;
        //                    if (_col.InputMask == string.Empty) _col.InputMask = "Number0";
        //                    _col.TextAlignment = TextAlignment.Far;
        //                    break;
        //                }
        //            case TypeCode.UInt64:
        //                {
        //                    if (_col.FormatString == string.Empty) _col.FormatString = Globals.NumberFormat.IntegerWithNull;
        //                    if (_col.InputMask == string.Empty) _col.InputMask = "Number0";
        //                    _col.TextAlignment = TextAlignment.Far;
        //                    break;
        //                }
        //            default:
        //                break;
        //        }
        //    }
        //}

        #endregion

        #region FormatGridView

        public static void AutoFormatGridView(DevExpress.XtraGrid.Views.Grid.GridView grid)
        {
            //foreach (DevExpress.XtraGrid.Columns.GridColumn item in grid.Columns)
            //{
            //    MessageBox.Show(item.ColumnType.ToString());
            //}
        }

        #endregion

        #region Show Message

        public static void ShowMessageBox(string mess, string caption)
        {
            MessageBox.Show(mess, caption);
        }

        #endregion

        #region Hash String

        public static string GetMD5(string chuoi)
        {
            string str_md5 = "";
            byte[] mang = System.Text.Encoding.UTF8.GetBytes(chuoi);

            MD5CryptoServiceProvider my_md5 = new MD5CryptoServiceProvider();
            mang = my_md5.ComputeHash(mang);

            foreach (byte b in mang)
            {
                str_md5 += b.ToString("X2");
            }

            return str_md5;
        }

        #endregion
    }
}
